package rbac.entity;

import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

import com.pandawork.core.entity.AbstractEntity;

/**
 * RBAC 角色 实体类.<br>
 * 记录了一个角色所具有权限信息.
 * 
 * @author Huadi
 * @author Zoufengyang
 */

@Entity
@Table(name = "t_rbac_role")
@NamedQueries( {
		@NamedQuery(name = "rbacRole.getAllRbacRoles", query = "from RbacRole"),
		@NamedQuery(name = "rbacRole.deleteRbacRoleById", query = "delete from RbacRole where id = :id") })
public class RbacRole extends AbstractEntity {

	private static final long serialVersionUID = -9033183014530480362L;

	// ---=== Fields ===--- //
	/** <code>Role</code> ID. */
	@Id
	@Column(name = "id", nullable = false, length = 11)
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private int id;

	/** <code>Role</code> name. */
	@Column(name = "name", nullable = false, length = 255)
	private String roleName;

	/** 该 <code>Role</code> 实体所拥有的 <code>Permission</code> (权限). */
	@ManyToMany(targetEntity = RbacPermission.class, fetch = FetchType.LAZY)
	@JoinTable(name = "t_rbac_role_permission", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name = "permission_id"))
	private Set<RbacPermission> permissions;

	/** 包含此角色 (<code>Role</code>) 的用户组 (<code>RbacUserGroup</code>) 实体. */
	@ManyToMany(targetEntity = RbacUserGroup.class, mappedBy = "roles", fetch = FetchType.LAZY)
	private Set<RbacUserGroup> userGroupsBelongsTo;

	// ---=== Getters and Setters ===--- //
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the roleName
	 */
	public String getRoleName() {
		return roleName;
	}

	/**
	 * @param roleName
	 *            the roleName to set
	 */
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	/**
	 * @return the permissions
	 */
	public Set<RbacPermission> getPermissions() {
		return permissions;
	}

	/**
	 * @param permissions
	 *            the permissions to set
	 */
	public void setPermissions(Set<RbacPermission> permissions) {
		this.permissions = permissions;
	}

	/**
	 * @return the userGroupsBelongsTo
	 */
	public Set<RbacUserGroup> getUserGroupsBelongsTo() {
		return userGroupsBelongsTo;
	}

	/**
	 * @param userGroupsBelongsTo
	 *            the userGroupsBelongsTo to set
	 */
	public void setUserGroupsBelongsTo(Set<RbacUserGroup> userGroupsBelongsTo) {
		this.userGroupsBelongsTo = userGroupsBelongsTo;
	}

}
